﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FrenzDenWebSite.Business;
using System.Xml.Serialization;
using System.Xml;
using System.Net.Mail;
using System.Net;

namespace FrenzDenWebSite
{
    public class SiteCommon
    {
        public static void LogError(string method, string message)
        {
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    ErrorLog log = new ErrorLog()
                    {
                        LoggedDate = DateTime.Now,
                        LogMessage = message,
                        MethodName = method
                    };

                    dataContext.ErrorLogs.InsertOnSubmit(log);
                    dataContext.SubmitChanges();
                }
            }
            catch
            {
            }
        }

        public static void SendMail(string toAddress, string subject, string body)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(toAddress);
                mail.From = new MailAddress("noreply@frenzden.com");
                mail.Subject = subject;
                mail.Body = body;

                SmtpClient smtp = new SmtpClient("mail.frenzden.com");
                smtp.Credentials = new NetworkCredential("noreply@frenzden.com", "DeeSha26$");

                smtp.Send(mail);
            }
            catch(Exception ex)
            {
                LogError("SendMail", ex.ToString());
            }
        }

        public static ProfileItem DecryptSession(string cookieValue)
        {
            return Deserialize(Crypto.DecryptStringAES(cookieValue)) as ProfileItem;
        }

        public static long GetLoggedInProfileID(string cookieValue)
        {
            return (Deserialize(cookieValue) as ProfileItem).ProfileID;
        }

        public static string EncryptSession(ProfileItem input)
        {
            string serializedString = Serialize(input);

            return Crypto.EncryptStringAES(serializedString);
        }

        public static string Serialize(ProfileItem input)
        {
            XmlSerializer ser = new XmlSerializer(input.GetType());
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            ser.Serialize(writer, input);
            return writer.ToString();
        }

        public static object Deserialize(string input)
        {
            ProfileItem Cat = new ProfileItem();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(input);
            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer ser = new XmlSerializer(Cat.GetType());
            return ser.Deserialize(reader);
        }
    }
}